home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / array / vbarray.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-06  |  9.5 KB  |  258 lines

  1. VERSION 2.00
  2. Begin Form VBArray 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "VB Array"
  5.    ClientHeight    =   3225
  6.    ClientLeft      =   1305
  7.    ClientTop       =   1500
  8.    ClientWidth     =   6330
  9.    Height          =   3630
  10.    Left            =   1245
  11.    LinkMode        =   1  'Source
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   3225
  15.    ScaleWidth      =   6330
  16.    Top             =   1155
  17.    Width           =   6450
  18.    Begin CommandButton OkCancel 
  19.       Cancel          =   -1  'True
  20.       Caption         =   "&Quit"
  21.       Height          =   420
  22.       Index           =   1
  23.       Left            =   3600
  24.       TabIndex        =   5
  25.       Top             =   2280
  26.       Width           =   1095
  27.    End
  28.    Begin CommandButton OkCancel 
  29.       Caption         =   "&OK"
  30.       Default         =   -1  'True
  31.       Height          =   420
  32.       Index           =   0
  33.       Left            =   1920
  34.       TabIndex        =   4
  35.       Top             =   2280
  36.       Width           =   1095
  37.    End
  38.    Begin OptionButton OtherDemo 
  39.       Caption         =   "&Some Type Array"
  40.       Height          =   255
  41.       Left            =   4200
  42.       TabIndex        =   3
  43.       Top             =   1800
  44.       Width           =   1815
  45.    End
  46.    Begin OptionButton LngIntDemo 
  47.       Caption         =   "&Long Integer Array"
  48.       Height          =   255
  49.       Left            =   2040
  50.       TabIndex        =   2
  51.       Top             =   1800
  52.       Width           =   1935
  53.    End
  54.    Begin OptionButton IntDemo 
  55.       Caption         =   "&Integer Array"
  56.       Height          =   255
  57.       Left            =   360
  58.       TabIndex        =   1
  59.       Top             =   1800
  60.       Value           =   -1  'True
  61.       Width           =   1455
  62.    End
  63.    Begin ListBox ListBox 
  64.       Height          =   1200
  65.       Left            =   360
  66.       TabIndex        =   0
  67.       Top             =   360
  68.       Width           =   5655
  69.    End
  70.    Begin Label Status 
  71.       Alignment       =   2  'Center
  72.       BorderStyle     =   1  'Fixed Single
  73.       Caption         =   "Status"
  74.       Height          =   255
  75.       Left            =   -120
  76.       TabIndex        =   6
  77.       Top             =   3000
  78.       Width           =   6615
  79.    End
  80. DefInt A-Z
  81. ' VBARRAY *FAST* file I/O of arrays
  82. ' Address questions/comments/improvements to
  83. ' Costas Kitsos, CIS ID: 73667,1755
  84. ' Enjoy!
  85. ' holds a Windows supplied temp filename for the demo
  86. Dim sTempFile As String
  87. ' listbox's hWnd so we can set tabs
  88. Dim hWndListBox As Integer
  89. ' Flag for Application Initialization
  90. Dim nInitApp As Integer
  91. Function DoIntDemo () As Integer
  92.      ' Declare variables
  93.      Dim tTempFile As OFSTRUCT     ' Used by OpenFile function
  94.      ReDim nBefore(50) As Integer  ' holds the Integers before I/O
  95.      ReDim nAfter(50) As Integer   ' holds the Integers after I/O
  96.      Dim nBytes, hFileOut, hFileIn, nIObytes, nFclose ' Integers
  97.      Dim lMes As Long, lFileSize As Long
  98.      ' Use the Random number generator to fill nBefore() with 50 integers
  99.      Randomize
  100.      For j = 1 To 50
  101.         nBefore(j) = Int((32767 - 1 + 1) * Rnd + 1)
  102.      Next
  103.     ' ** I/O Starts here
  104.     ' Create our temp file and open it for writing
  105.     hFileOut = OpenFile(sTempFile, tTempFile, OF_CREATE Or OF_WRITE)
  106.     ' If we have a file handle proceed
  107.     If hFileOut <> 0 Then
  108.     ' calculate the number of bytes to be written (NumberOfElements * Size)
  109.     ' Since we want to write the entire array the formula would be:
  110.     ' nBytes = UBound(Array) * Len(Array(element)).  If we only needed
  111.     ' twenty elements then nBytes = 20 * Len(Array(element)).  Since we're
  112.     ' dealing with fixed length arrays, element can be any legitimate
  113.     ' array element.
  114.         nBytes = UBound(nBefore) * Len(nBefore(1))
  115.           
  116.     ' Write nBefore() to disk using API's lwrite function.
  117.     ' We only need to pass the first array element.  The nBytes
  118.     ' parameter tells Windows how many bytes to write, or as far
  119.     ' as we're concerned how many array elements.
  120.         nIObytes = lwrite(hFileOut, nBefore(1), nBytes)
  121.     ' Get the file size with llseek.  By specifying 0 for lOffset and
  122.     ' and 2 for iOrigin we're saying seek position 0 from the end of
  123.     ' the file.  In other words, give us the FileSize.
  124.         lFileSize = llseek(hFileOut, 0, 2)
  125.     ' close the output file.
  126.         nFclose = lclose(hFileOut)
  127.     ' Now let's see if it worked.  Open the file for reading.
  128.         hFileIn = OpenFile(sTempFile, tTempFile, OF_READ)
  129.     ' We'll use the nAfter() array this time, nBytes is the same.
  130.         nIObytes = lread(hFileIn, nAfter(1), nBytes)
  131.     ' close the input file.
  132.         nFclose = lclose(hFileIn)
  133.     ' Let's prove that it worked.  First, clear the list box.
  134.         
  135.         lMes = SendMessage(hWndListBox, LB_RESETCONTENT, 0, ByVal 0&)
  136.     ' Add a title.
  137.         ListBox.AddItem "Before" + Chr$(9) + "After"
  138.     ' Add the nBefore() and nAfter() contents to the listbox
  139.         For j = 1 To 50
  140.         ListBox.AddItem LTrim$(Str$(nBefore(j))) + Chr$(9) + LTrim$(Str$(nAfter(j)))
  141.         Next
  142.         
  143.         Status.Caption = "Temp File: " + sTempFile + Str$(lFileSize) + " bytes"
  144.         DoIntDemo = True    ' success
  145.     Else
  146.     DoIntDemo = False       ' failure
  147.     End If
  148. End Function
  149. Function DoLngIntDemo () As Integer
  150.        ' Please see the DoIntDemo function for comments
  151.     Dim tTempFile As OFSTRUCT
  152.     ReDim lBefore(50) As Long
  153.     ReDim lAfter(50) As Long
  154.     Dim nBytes, hFileOut, hFileIn, nIObytes, nFclose
  155.     Dim lMes As Long, lFileSize As Long
  156.     Randomize
  157.     For j = 1 To 50
  158.        lBefore(j) = Int(1234532767 * Rnd + 1)
  159.     Next
  160.     hFileOut = OpenFile(sTempFile, tTempFile, OF_CREATE Or OF_WRITE)
  161.     If hFileOut <> 0 Then
  162.         nBytes = UBound(lBefore) * Len(lBefore(1))
  163.         nIObytes = lwrite(hFileOut, lBefore(1), nBytes)
  164.         lFileSize = llseek(hFileOut, 0, 2)
  165.         nFclose = lclose(hFileOut)
  166.                
  167.         hFileIn = OpenFile(sTempFile, tTempFile, OF_READ)
  168.         nIObytes = lread(hFileIn, lAfter(1), nBytes)
  169.         nFclose = lclose(hFileIn)
  170.         lMes = SendMessage(hWndListBox, LB_RESETCONTENT, 0, ByVal 0&)
  171.         ListBox.AddItem "Before" + Chr$(9) + "After"
  172.         For j = 1 To 50
  173.         ListBox.AddItem LTrim$(Str$(lBefore(j))) + Chr$(9) + LTrim$(Str$(lAfter(j)))
  174.         Next
  175.         Status.Caption = "Temp File: " + sTempFile + Str$(lFileSize) + " bytes"
  176.         
  177.         DoLngIntDemo = True
  178.     Else
  179.     DoLngIntDemo = False
  180.     End If
  181. End Function
  182. Function DoOtherDemo () As Integer
  183.      ' Please see the DoIntDemo function for comments
  184.     Dim tTempFile As OFSTRUCT
  185.     ReDim tBefore(50) As SomeType
  186.     ReDim tAfter(50) As SomeType
  187.     Dim nBytes, hFileOut, hFileIn, nIObytes, nFclose
  188.     Dim lMes As Long, lFileSize As Long
  189.     Randomize
  190.     For j = 1 To 50
  191.         tBefore(j).nInteger = Int((32767 - 1 + 1) * Rnd + 1)
  192.         tBefore(j).sString = Chr$(Int((90 - 65 + 1) * Rnd + 65)) + "abcde"
  193.         tBefore(j).lLong = Int(1232767 * Rnd + 1)
  194.     Next
  195.     hFileOut = OpenFile(sTempFile, tTempFile, OF_CREATE Or OF_WRITE)
  196.     If hFileOut <> 0 Then
  197.         nBytes = UBound(tBefore) * Len(tBefore(1))
  198.         nIObytes = lwrite(hFileOut, tBefore(1), nBytes)
  199.         lFileSize = llseek(hFileOut, 0, 2)
  200.         nFclose = lclose(hFileOut)
  201.         hFileIn = OpenFile(sTempFile, tTempFile, OF_READ)
  202.         nIObytes = lread(hFileIn, tAfter(1), nBytes)
  203.         nFclose = lclose(hFileIn)
  204.         lMes = SendMessage(hWndListBox, LB_RESETCONTENT, 0, ByVal 0&)
  205.         ListBox.AddItem "Before" + Chr$(9) + "After"
  206.         For j = 1 To 50
  207.         Before$ = LTrim$(Str$(tBefore(j).nInteger)) + " " + tBefore(j).sString + Str$(tBefore(j).lLong)
  208.         After$ = LTrim$(Str$(tAfter(j).nInteger)) + " " + tAfter(j).sString + Str$(tAfter(j).lLong)
  209.         ListBox.AddItem Before$ + Chr$(9) + After$
  210.         Next
  211.         Status.Caption = "Temp File: " + sTempFile + Str$(lFileSize) + " bytes"
  212.         DoOtherDemo = True
  213.     Else
  214.     DoOtherDemo = False
  215.     End If
  216. End Function
  217. Sub Form_Load ()
  218.     sTempFile = String$(144, 0)  ' Buffer
  219.     ' Ask Windows for a temp file.
  220.     nResult = GetTempFileName(0, "VBA", 0, sTempFile)
  221.     sTempFile = Left$(sTempFile, InStr(sTempFile, Chr$(0)) - 1)
  222.     Status.Caption = "Temp File: " + sTempFile
  223.     nInitApp = True
  224. End Sub
  225. Sub Form_Paint ()
  226.     If nInitApp = True Then
  227.     ListBox.SetFocus
  228.     hWndListBox = GetFocus()  ' get the listbox's hWnd
  229.     ' Set 1 tab stop at position 105
  230.     Mes& = SendMessage(hWndListBox, LB_SETTABSTOPS, 1, 105)
  231.     OkCancel(0).SetFocus
  232.     nInitApp = False  'set the flag to FALSE so we don't repeat this
  233.     End If
  234. End Sub
  235. Sub Form_Unload (Cancel As Integer)
  236.     Dim tTempFile As OFSTRUCT   ' Used by OpenFile
  237.    ' If our temp file exists, delete it.
  238.     If OpenFile(sTempFile, tTempFile, OF_EXIST) <> 0 Then nRemoveTemp = OpenFile(sTempFile, tTempFile, OF_DELETE)
  239.     End
  240. End Sub
  241. Sub OkCancel_Click (Index As Integer)
  242.     If Index = 0 Then
  243.             
  244.     If IntDemo.Value = True Then  ' Do the Integer Array demo
  245.         If Not DoIntDemo() Then MsgBox "Integer Demo Failed", 16, VBARRAY.Caption
  246.     End If
  247.         
  248.     If LngIntDemo.Value = True Then  ' Do the Long Integer Array demo
  249.         If Not DoLngIntDemo() Then MsgBox "Long Integer Demo Failed", 16, VBARRAY.Caption
  250.     End If
  251.     If OtherDemo.Value = True Then   ' Do the SomeType Array demo
  252.         If Not DoOtherDemo() Then MsgBox "Some Type Demo Failed", 16, VBARRAY.Caption
  253.     End If
  254.     Else
  255.     Call Form_Unload(0)  ' Quit pressed
  256.     End If
  257. End Sub
  258.